package com.vesper.mysdk

import android.util.Log


class MSDKImpl : MSDK {
    val TAG: String = "MSDKImpl"
    var callbacks = ArrayList<MCallback>()
    var status: Int = STATUS_NOT_INIT

    constructor(secretKey: String, appKey: String, callback: MCallback) {
        if (!callbacks.contains(callback)) {
            callbacks.add(callback)
        }
        //模拟校验秘钥
        if (checkKeys(secretKey, appKey)) {
            notifyInit(true, REASON_NORMAL)
            var old = this.status
            this.status = STATUS_INITED
            notifyStatusChanges(old, this.status)
        } else {
            notifyInit(false, MREASON_ACCOUNT_OR_PASSWORD_WRONG)
            callbacks.remove(callback)
        }
    }

    override fun getLoginStatus(): Int {

        return this.status
    }


    override fun login(uid: String, password: String) {
        if (this.status <= STATUS_NOT_INIT) {
            notifyLogin(false, REASON_NOT_INIT)
        } else if (this.status == STATUS_INITED || this.status == STATUS_NOT_LOGIN) {
            var old = this.status
            this.status = STATUS_LOGINED
            notifyStatusChanges(old, this.status)
            notifyLogin(true, REASON_NORMAL)
        } else if (this.status > STATUS_NOT_LOGIN) {
            notifyLogin(false, REASON_ALREADY_LOGIN)
            Log.e(TAG, "login: sdk already logined!")
        }

    }

    override fun logout() {
        if (this.status == STATUS_LOGINED) {
            var old = this.status
            this.status = STATUS_NOT_LOGIN
            notifyLogout(true, REASON_NORMAL)
            notifyStatusChanges(old, this.status)
        } else {
            notifyLogout(false, REASON_NOT_LOGIN)
        }
    }

    override fun destroySDK() {
        Log.i(TAG, "destroy")
        if (this.status > 0) {
            //上报状态改变
            var old = this.status
            this.status = STATUS_NOT_INIT
            notifyStatusChanges(old, this.status)
            notifyLogout(true, REASON_NORMAL)//上报sdk退出登录
            callbacks.clear()
        }

    }


    fun notifyInit(res: Boolean, @Companion.MReason reason: Int) {

        if (callbacks.size > 0) {
            for (callback in callbacks) {
                callback.onInit(res, reason)
            }
        }
    }

    fun notifyLogin(res: Boolean, @Companion.MReason reason: Int) {
        Log.i(TAG, "notifyLogin: res=$res,reason=$reason")
        if (callbacks.size > 0) {
            for (callback in callbacks) {
                callback.onLogin(res, reason)
            }
        }
    }

    fun notifyLogout(res: Boolean, @Companion.MReason reason: Int) {
        Log.i(TAG, "notifyLogout: res=$res,reason=$reason")
        if (callbacks.size > 0) {
            for (callback in callbacks) {
                callback.onLogout(res, reason)
            }
        }
    }


    fun notifyStatusChanges(@Companion.SDKStatus old: Int, @Companion.SDKStatus new: Int) {
        Log.i(TAG, "notifyStatusChanges: oldStatus=$old,newStatus=$new")
        if (callbacks.size > 0) {
            for (callback in callbacks) {
                callback.onLoginStatusChanged(old, new)
            }
        }
    }

    /**
     * 模拟校验秘钥
     *
     * @param secretKey
     * @param appKey
     * @return
     */
    fun checkKeys(secretKey: String?, appKey: String?): Boolean {
        if (secretKey != "123") {
            Log.e(TAG, "checkKeys: secreKey is not right")
            return false
        }
        if (appKey != "123") {
            Log.e(TAG, "checkKeys: secreKey is not right")
            return false
        }
        return true
    }
}